package com.yanqu.road.dao.impl.activity.cat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.cat.Element;
import com.yanqu.road.entity.activity.cat.GameData;
import com.yanqu.road.entity.activity.cat.data.CatUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class CatUserDataDaoImpl extends TempDataObjectDao<CatUserData> {


    public static String getCatUserDataSql = "select * from `t_u_activity_cat_user_data` where `activityId`=? and `userId`=?";
    public CatUserData getCatUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getCatUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertCatUserDataSql = "insert into `t_u_activity_cat_user_data`(activityId,userId,score,energy,lastRecTime,abilityLv,ability,damage,passStarMap,gameData,cellData,useEnergy,gameTimes," +
            "historyPassId) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CatUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAbilityLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAbility()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDamage()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPassStarMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGameData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCellData())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUseEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGameTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHistoryPassId()));
        if (getDb().executeNoneQuery(insertCatUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCatUserDataSql = "update `t_u_activity_cat_user_data` set score=?,energy=?,lastRecTime=?,abilityLv=?,ability=?,damage=?,passStarMap=?,gameData=?,cellData=?,useEnergy=?,gameTimes=?," +
            "historyPassId=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(CatUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAbilityLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAbility()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDamage()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPassStarMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGameData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCellData())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUseEnergy()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGameTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHistoryPassId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateCatUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }


    @Override
    protected CatUserData parseData(ResultSet rs) throws SQLException {
        CatUserData catUserData = new CatUserData();
        catUserData.setActivityId(rs.getInt("activityId"));
        catUserData.setUserId(rs.getLong("userId"));
        catUserData.setScore(rs.getInt("score"));
        catUserData.setEnergy(rs.getInt("energy"));
        catUserData.setLastRecTime(rs.getLong("lastRecTime"));
        catUserData.setAbilityLv(rs.getInt("abilityLv"));
        catUserData.setAbility(rs.getInt("ability"));
        catUserData.setDamage(rs.getLong("damage"));
        catUserData.setPassStarMap(JSON.parseObject(rs.getString("passStarMap"), new TypeReference<Map<Integer, Integer>>() {}));
        catUserData.setGameData(JSON.parseObject(rs.getString("gameData"), GameData.class));
        catUserData.setCellData(JSON.parseObject(rs.getString("cellData"), Element[][].class));
        catUserData.setUseEnergy(rs.getInt("useEnergy"));
        catUserData.setGameTimes(rs.getInt("gameTimes"));
        catUserData.setHistoryPassId(rs.getInt("historyPassId"));
        return catUserData;
    }
}
